package com.yhsoft.bill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.bill.controller.request.SysCzInVo;
import com.yhsoft.bill.controller.request.SysYfzkInVo;
import com.yhsoft.bill.controller.response.SysYfzkOutVo;
import com.yhsoft.bill.controller.response.VenderCwtjOutVo;
import com.yhsoft.bill.entity.SysYfzk;
import com.yhsoft.bill.mapper.SysYfzkMapper;
import com.yhsoft.bill.service.ISysYfzkService;
import com.yhsoft.common.service.TokenService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 平台应付账款 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-11-03
 */
@Service
public class SysYfzkServiceImpl extends ServiceImpl<SysYfzkMapper, SysYfzk> implements ISysYfzkService {

    @Override
    public void addYszk(Integer companyId) {
        SysYfzk yfzk = new SysYfzk();
        yfzk.setCompanyId(companyId);
        yfzk.setTotalYfAmount(BigDecimal.ZERO);
        yfzk.setTotalPayAmount(BigDecimal.ZERO);
        this.getBaseMapper().insert(yfzk);

    }

    @Override
    public IPage<SysYfzkOutVo> getYfzkList(SysYfzkInVo inVo, Long pageNo, Long pageSize) {
        QueryWrapper<SysYfzkOutVo> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasLength(inVo.getPayBy()), "a.pay_by", inVo.getPayBy());
        wrapper.eq(StringUtils.hasLength(inVo.getVenderId()), "a.company_id", inVo.getVenderId());

        wrapper.orderByDesc("a.create_time");

        Page<SysYfzkOutVo> page = new Page<>(pageNo, pageSize);
        IPage<SysYfzkOutVo> iPage = baseMapper.getYfzkList(page, wrapper);

        return iPage;
    }

    @Override
    public void cz(SysCzInVo inVo) {
        SysYfzk yfzk = this.getBaseMapper().selectById(inVo.getId());
        if(yfzk == null){
            return;
        }
        // 累计已付金额
        BigDecimal totalPay = yfzk.getTotalPayAmount();
        yfzk.setTotalPayAmount(totalPay.add(inVo.getPayAmount()));
        this.getBaseMapper().updateById(yfzk);
    }

    @Override
    public VenderCwtjOutVo cwtj() {
        VenderCwtjOutVo outVo = new VenderCwtjOutVo();
        LambdaQueryWrapper<SysYfzk> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysYfzk::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        wrapper.orderByDesc(SysYfzk::getCreateTime);
        List<SysYfzk> list = this.list(wrapper);
        if(list == null || list.size() == 0){
            return outVo;
        }
        SysYfzk yfzk = list.get(0);
        outVo.setTotalPayAmount(yfzk.getTotalPayAmount());
        outVo.setTotalYfAmount(yfzk.getTotalYfAmount());
        outVo.setTotalNotPayAmount(yfzk.getTotalYfAmount().subtract(yfzk.getTotalPayAmount()));

        return outVo;
    }

    @Override
    public void addYfzk(BigDecimal totalOriginalPrice, Integer vendorId) {
        LambdaQueryWrapper<SysYfzk> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysYfzk::getCompanyId, vendorId);
        SysYfzk sysYfzk = this.getOne(wrapper);
        if(sysYfzk != null){
            sysYfzk.setTotalYfAmount(sysYfzk.getTotalYfAmount().add(totalOriginalPrice));
            this.updateById(sysYfzk);
        }
    }
}
